<?php
namespace AppHome\Controller;
use Think\Controller;
class UserController extends Controller {
    /*APP提交功能首页*/
    public function index(){
        $this->display();
    }
    public function login(){
        $this->display();
    }
    public function register(){
        $this->display();
    }
    public function register_exec(){
        $data['username']=I('post.username');
        $data['password']=md5(I('post.password'));
        $data['idnum']=I('post.idnum');
        $data['phone']=I('post.phone');
        $data['time'] = date('Y-m-d',time());
        $vali = M('driver')->where(array('phone'=>I('post.phone')))->find();
        if($vali){
            echo json_encode(array('code'=>101));
            die();
        }
        if($_SESSION['code'] != I('post.code')){
            echo json_encode(array('code'=>102,'msg'=>'验证码不正确'));
            die();
        }
        $result = M('driver')->add($data);
        if($result){
            $_SESSION['user']['userid'] =$result;
            $_SESSION['user']['username'] = $data['username'];
            $_SESSION['user']['phone'] = $data['phone'];
            echo json_encode(array('code'=>100,'msg'=>'注册成功'));

        }

    }
    /*登陆操作*/
    public function login_exec(){
        $phone =  I('post.phone');
        $pwd =  I('post.password');
        $result = M('driver')->where(array('phone'=>$phone))->find();
        if($result){
            if(md5($pwd) == $result['password']){
                $_SESSION['user']['userid'] = $result['userid'];
                $_SESSION['user']['username'] = $result['username'];
                $_SESSION['user']['phone'] = $result['phone'];
                echo json_encode(array('code'=>'200'));
            }else{
                echo json_encode(array('code'=>'201'));
                die();
            }
        }else{
            echo json_encode(array('code'=>'202'));
            die();
        }
    }
    /*短信接口调用*/
    public function sms_send(){
        $mobile = I('get.mobile');
        if(empty($mobile)){
            echo json_encode(array('code'=>201,'msg'=>'号码不能为空！'));
            exit;
        }
        $code = rand(0000,9999);
        $target = "http://121.199.16.178/webservice/sms.php?method=Submit";
        $post_data = "account=cf_yisheshiye&password=fd3373ffe5c1fea369a5f06ef806ddc9&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$code."。请不要把验证码泄露给其他人。");
        $result = $this->xml_to_array($this->Post($post_data, $target));
        if($result['SubmitResult']['code'] == 2){
            $_SESSION['code'] = $code;
            echo json_encode(array('code'=>100,'msg'=>'发送成功'));exit;
        }
        else{
            echo json_encode(array('code'=>$result['SubmitResult']));exit;
        }

    }
    function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }


}